package servlets;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import exception.ValidationException;

import tools.beans.Md5Bean;

import managers.EntityManagerAux;


/**
 * Servlet implementation class AddUser
 * Servlet pour ajouter les utilisateurs, apres etre ajoute, les 
 * utilisateurs seront validees par un administrateur quelconque.
 */
public class AddUser extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddUser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    @EJB EntityManagerAux mng;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String id = request.getParameter("idUser");
		String name = request.getParameter("nameUser");
		String surname = request.getParameter("surnameUser");
		String email = request.getParameter("mailUser");
		String sex = request.getParameter("sexUser");
		String password = request.getParameter("passUser");
		String age = request.getParameter("ageUser");
		
		String goTo="";
		boolean hasError = false;
	    String erreurs = "";

	    //New code
	    if (id.equals(null) || id.isEmpty()) {
            hasError = true;
            erreurs += "<li>Login incorrect</li>";
        }
	    if (name.equals(null) || "".equals(name)) {
            hasError = true;
            erreurs += "<li>Nom incorrect</li>";
        }
	    if (surname.equals(null) || "".equals(surname)) {
            hasError = true;
            erreurs += "<li>Prenom incorrect</li>";
        }
	    if (password.equals(null) || "".equals(password) || password.length() < 6) {
            hasError = true;
            erreurs += "<li>Le mot de passe doit contenir au minimum 6 caractères</li>";
        }
	    if (email.equals(null) || "".equals(email)) {
            hasError = true;
            erreurs += "<li>Email incorrect</li>";
        }	    
	    if(sex.isEmpty()){
	    	 hasError = true;
	         erreurs += "<li>Sex incorrect</li>";
	    }
	    if (age.equals(null) || "".equals(age)) {
            hasError = true;
            erreurs += "<li>Age incorrect</li>";
        }
	    
	    if (hasError) {
            throw new ValidationException(erreurs);
            //Code momentaneo
		
        }

	    try {
			int aux = Integer.parseInt(age);
			
			String passHash = mng.encode(password);
			
			mng.addUser(id, name, passHash, surname, email, sex,age);
			goTo="Succes.jsp";
			request.getRequestDispatcher(goTo).forward(request, response);
		 } catch (NumberFormatException e) {
			System.err.println("Error, not a number");
			goTo = "Error.jsp"; //Error, at least one field is null
			request.getRequestDispatcher(goTo).forward(request, response);
		   }	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
